package app.itw.tison.cloud.product_service.controller;


import app.itw.tison.cloud.model.Product;
import app.itw.tison.cloud.product_service.service.ProductService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单服务
 */
@RestController
@RequestMapping("/api/v1/product")
public class ProductController {

  @Resource
  private ProductService productService;

  @GetMapping("get")
  public Object getProduct(@RequestParam(value = "id") String id) {
    Product product = productService.getProductByID(id);
    return product;
  }

  @PostMapping("gets")
  public Object getProducts(@RequestBody List<HashMap<String, Object>> list) {
    ArrayList<String> ids = new ArrayList<>();
    try {
      list.forEach(item->{
        item.forEach((key,value)->{
          ids.add(key);
        });
      });
      return productService.getProductByIds(ids);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  @GetMapping("list")
  public Object getProductList() {
    List<Product> list = productService.getProductList();
    return list;
  }

}
